package com.example.myapplication.activity;

import android.graphics.drawable.Drawable;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import com.example.myapplication.App;
import com.example.myapplication.R;
import com.example.myapplication.adapter.FragmentAdapter;
import com.example.myapplication.base.BaseActivity;
import com.example.myapplication.fragment.HomeFragment;
import com.example.myapplication.fragment.NewsFragment;
import com.example.myapplication.fragment.PersonFragment;
import com.example.myapplication.fragment.ServiceFragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity {

    private Drawable[] naviDrawable = new Drawable[4];
    private Drawable[] naviPressDrawable = new Drawable[4];
    private AppCompatTextView[] naviTextView = new AppCompatTextView[4];
    private AppCompatTextView titleTextView = null;
    private ViewPager2 mainViewPager2 = null;

    @Override
    public void initView() {

        setContentView(R.layout.activity_main);
        naviTextView[0] = findViewById(R.id.homeTextView);
        naviTextView[1] = findViewById(R.id.serviceTextView);
        naviTextView[2] = findViewById(R.id.newsTextView);
        naviTextView[3] = findViewById(R.id.personTextView);
        titleTextView = findViewById(R.id.titleTextView);
        mainViewPager2 = findViewById(R.id.mainViewPager2);

    }

    @Override
    public void initData() {

        naviDrawable[0] = getDrawable(R.drawable.ic_main_navi_home);
        naviDrawable[1] = getDrawable(R.drawable.ic_main_navi_service);
        naviDrawable[2] = getDrawable(R.drawable.ic_main_navi_news);
        naviDrawable[3] = getDrawable(R.drawable.ic_main_navi_person);
        naviPressDrawable[0] = getDrawable(R.drawable.ic_main_navi_home_press);
        naviPressDrawable[1] = getDrawable(R.drawable.ic_main_navi_service_press);
        naviPressDrawable[2] = getDrawable(R.drawable.ic_main_navi_news_press);
        naviPressDrawable[3] = getDrawable(R.drawable.ic_main_navi_person_press);
        List<Fragment> fragmentList = new ArrayList<>();
        fragmentList.add(new HomeFragment());
        fragmentList.add(new ServiceFragment());
        fragmentList.add(new NewsFragment());
        fragmentList.add(new PersonFragment());
        mainViewPager2.setOrientation(ViewPager2.ORIENTATION_HORIZONTAL);//左右滑动
        //mainViewPager2.setOrientation(ViewPager2.ORIENTATION_HORIZONTAL);//类似抖音的上下滑动视频流
        mainViewPager2.setAdapter(new FragmentAdapter(this, fragmentList));
        mainViewPager2.setOffscreenPageLimit(naviTextView.length);

    }

    @Override
    public void initEvent() {

        for (int i = 0; i < naviTextView.length; i++) {
            final int position = i;
            naviTextView[i].setOnClickListener(view -> setNavi(position, false));
        }

        mainViewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                setNavi(position, true);
                super.onPageSelected(position);
            }
        });

    }

    public void setNavi(int position, boolean isViewPager) {

        //重置所有控件的状态
        for (int i = 0; i < naviTextView.length; i++) {
            naviTextView[i].setCompoundDrawablesWithIntrinsicBounds(null, naviDrawable[i], null, null);
            naviTextView[i].setTextColor(getColor(R.color.tips));
        }
        //设置选中的标签的图标颜色跟文字颜色
        naviTextView[position].setCompoundDrawablesWithIntrinsicBounds(null, naviPressDrawable[position], null, null);
        naviTextView[position].setTextColor(getColor(R.color.purple_500));
        titleTextView.setText(naviTextView[position].getText());
        if (!isViewPager) mainViewPager2.setCurrentItem(position);

    }

}
